package aiball

import org.springframework.dao.DataIntegrityViolationException
import grails.plugins.springsecurity.Secured
import components.GlobalConstants;
import aiball.GamechartsReaderService

class GamechartsController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }
	
	def updateTables(){
		def leser = new GamechartsReaderService();
		leser.read();
		leser.untaetige();
		def top3=new Top3StatsCalculatorService();
		top3.calculate();
		def deleteHistory=new DeleteHistoryService();
		deleteHistory.deleteHistory();
		render(leser.toString());
	}
	
	
    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[gamechartsInstanceList: Gamecharts.list(params), gamechartsInstanceTotal: Gamecharts.count()]
    }

	def downloadXML() {
		def game = Gamecharts.get(params.get('xmlid'))
		def aus = Gamecharts.get(params.get('xmlid')).getGameXML().toString()
		response.addHeader("Content-disposition","attachment; filename=GameXML_" + game.team1.name + "_" + game.team1Goals + "_" + game.team2.name + "_" + game.team2Goals + ".xml");
		render(text: aus, cntentType: "text/xml")

	}
	
	def playXML() {
		def aus = Gamecharts.get(params.get('xmlid')).getGameXML().toString()
		render(text:aus,contentType:"text/xml",encoding:"UTF-8")
		

	}
	
	def playGamePane()
	{
		def xmlid = params.get('xmlid');
		String viewer = "";
		FileReader fr = new FileReader( GlobalConstants.PATH_VIEWER + "/viewer.html"); 
		BufferedReader br = new BufferedReader(fr); 
		String s; 
		while((s = br.readLine()) != null) { 
		viewer += s + "\n";
		} 
		
		fr.close();  
		viewer = viewer.replace("{MYOUTPUTXML}", "playXML?xmlid=" + xmlid);
		render(viewer);
	}
	
	@Secured(['ROLE_ADMIN'])
    def create() {
        [gamechartsInstance: new Gamecharts(params)]
    }
	
	@Secured(['ROLE_ADMIN'])
    def save() {
        def gamechartsInstance = new Gamecharts(params)
        if (!gamechartsInstance.save(flush: true)) {
            render(view: "create", model: [gamechartsInstance: gamechartsInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'gamecharts.label', default: 'Gamecharts'), gamechartsInstance.id])
        redirect(action: "show", id: gamechartsInstance.id)
    }

    def show() {
        def gamechartsInstance = Gamecharts.get(params.id)
        if (!gamechartsInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'gamecharts.label', default: 'Gamecharts'), params.id])
            redirect(action: "list")
            return
        }
		def date = gamechartsInstance.time.toString()
		date = date.replace(' 00:00:00.0', '')
		String u = GlobalConstants.URL_COMPLEATE + "/gamecharts/playGamePane?xmlid=" + gamechartsInstance.id;
        [gamechartsInstance: gamechartsInstance , iframeurl: u , date : date]
    }
	
	@Secured(['ROLE_ADMIN'])
    def edit() {
        def gamechartsInstance = Gamecharts.get(params.id)
        if (!gamechartsInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'gamecharts.label', default: 'Gamecharts'), params.id])
            redirect(action: "list")
            return
        }

        [gamechartsInstance: gamechartsInstance]
    }
	
	@Secured(['ROLE_ADMIN'])
    def update() {
        def gamechartsInstance = Gamecharts.get(params.id)
        if (!gamechartsInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'gamecharts.label', default: 'Gamecharts'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (gamechartsInstance.version > version) {
                gamechartsInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'gamecharts.label', default: 'Gamecharts')] as Object[],
                          "Another user has updated this Gamecharts while you were editing")
                render(view: "edit", model: [gamechartsInstance: gamechartsInstance])
                return
            }
        }

        gamechartsInstance.properties = params

        if (!gamechartsInstance.save(flush: true)) {
            render(view: "edit", model: [gamechartsInstance: gamechartsInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'gamecharts.label', default: 'Gamecharts'), gamechartsInstance.id])
        redirect(action: "show", id: gamechartsInstance.id)
    }
	
	@Secured(['ROLE_ADMIN'])
    def delete() {
        def gamechartsInstance = Gamecharts.get(params.id)
        if (!gamechartsInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'gamecharts.label', default: 'Gamecharts'), params.id])
            redirect(action: "list")
            return
        }

        try {
            gamechartsInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'gamecharts.label', default: 'Gamecharts'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'gamecharts.label', default: 'Gamecharts'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
	
	def showSeasonGames() {
		[gamechartsInstanceList: Gamecharts.list(params), gamechartsInstanceTotal: Gamecharts.count(), datum : params.get('date')]
	}
}
